//
//  GYSystemMessageViewController.m
//  GoodLuck
//
//  Created by apple on 2018/3/19.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYSystemMessageViewController.h"
#import "GYNetWorkTools.h"
#import "GYNoAddressView.h"
#import "GYSearchViewController.h"
#import "GYBasePopoverViewController.h"
#import "PopoverAnimation.h"
#import "GYSystemMsg.h"
#import "GYSystemMsgCell.h"
#import "GYShopingCartViewController.h"

@interface GYSystemMessageViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;

/** 没有消息视图 */
@property (nonatomic, strong) GYNoAddressView *NoSystemMessageView;

/** 动画代理 */
@property (nonatomic, strong) PopoverAnimation *animationDelegate;

/** 数据源 */
@property (nonatomic, strong) NSArray *msgList;
@end

@implementation GYSystemMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"消息列表";
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"more_b" tagret:self action:@selector(moreBtuClick) button:nil];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    //加载数据
    [self loadSystemMessage];
}


- (void)loadSystemMessage {
    [GYSystemMsg loadSystemMessage:^(NSArray * _Nullable msgList, NSError * _Nullable error) {
        if (!error) {
            if (msgList.count == 0) {
                if (self.NoSystemMessageView == nil) {
                    [self getNoSystemMessageView];
                }else {
                    self.NoSystemMessageView.hidden = NO;
                }
            }else {
                self.NoSystemMessageView.hidden = YES;
                
                //显示消息
                self.msgList = msgList;
                [self.tableView reloadData];
            }
        }
    }];
}

- (void)moreBtuClick {
    GYBasePopoverViewController *basePopoverVC = [[GYBasePopoverViewController alloc] initWithNibName:@"GYBasePopoverViewController" bundle:nil];
    __weak typeof(self) weakSelf = self;
    basePopoverVC.popoverViewSelectIndexCallBack = ^(NSInteger index) {
        switch (index) {
            case 0:
            {
                [weakSelf.navigationController popViewControllerAnimated:YES];
            }
                break;
                
            case 1:
            {
                GYSearchViewController *searchVC = [[GYSearchViewController alloc] init];
                [weakSelf.navigationController pushViewController:searchVC animated:YES];
            }
                break;
                
            case 2:
            {
                GYShopingCartViewController *shopingCartVC = [[GYShopingCartViewController alloc] initWithNibName:@"GYShopingCartViewController" bundle:nil];
                [weakSelf.navigationController pushViewController:shopingCartVC animated:YES];
            }
                break;
                
            case 3:
            {
                weakSelf.tabBarController.selectedIndex = 2;
            }
                break;
                
            default:
                break;
        }
    };
    basePopoverVC.titles = @[@"首页",@"搜索",@"购物车",@"我的商城"];
    basePopoverVC.images = @[@"home_w",@"search_w",@"ff_icon3",@"ff_icon4"];
    
    self.animationDelegate = [[PopoverAnimation alloc] init];
    self.animationDelegate.popoverFrame = CGRectMake(ScreenWidth-widthScale(130, iphone6_W)-5, 50, widthScale(130, iphone6_W), basePopoverVC.titles.count*50);
    basePopoverVC.transitioningDelegate = (id)weakSelf.animationDelegate;
    
    basePopoverVC.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:basePopoverVC animated:YES completion:nil];

}

#pragma mark -- UITableViewDataSource,UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.msgList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GYSystemMsgCell *cell = [GYSystemMsgCell cellWithTableView:tableView];
    cell.model = self.msgList[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    GYSystemMsgCell *cell =[GYSystemMsgCell cellWithTableView:tableView];
    
    return [cell height:self.msgList[indexPath.row]];
}

- (void)getNoSystemMessageView {
    
    GYNoAddressView *NoSystemMessageView = [[[NSBundle mainBundle] loadNibNamed:@"GYNoAddressView" owner:nil options:nil] firstObject];
    NoSystemMessageView.frame = self.view.bounds;
    NoSystemMessageView.markImageView.image = [UIImage imageNamed:@"message_w"];
    NoSystemMessageView.headLable.text = @"您还没有系统消息";
    NoSystemMessageView.footLable.hidden = YES;
    NoSystemMessageView.addBtu.hidden = YES;
    [self.view addSubview:NoSystemMessageView];
    
    self.NoSystemMessageView = NoSystemMessageView;
}

@end
